package com.zqw.controller;

import com.zqw.info.GoodsInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.info.UserInfo;
import com.zqw.service.GoodsService;
import com.zqw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ShopController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    //获取购物车并将购物车返回给前端
    @RequestMapping("car")
    public ResponseMsg car(HttpSession session){
        ResponseMsg responseMsg = new ResponseMsg();
        List<GoodsInfo> shopCt = (List<GoodsInfo>) session.getAttribute("shopCt");
        if(null == shopCt){
            shopCt = new ArrayList<>();
        }
        String goods = getGoods(shopCt);
        responseMsg.setResult("0000");
        responseMsg.setList(shopCt);
        responseMsg.setResultMsg(goods);
        return responseMsg;
    }

    //删除购物车中的一组购物项
    @RequestMapping("removeCar")
    public ResponseMsg removeCar(String gid, HttpSession session){
        ResponseMsg responseMsg = new ResponseMsg();
        if(gid!=null){
            List<GoodsInfo> shopCt = (List<GoodsInfo>) session.getAttribute("shopCt");
            UserInfo user = (UserInfo)session.getAttribute("user");
            for (int i = 0; i < shopCt.size(); i++) {
                if(shopCt.get(i).getGoodsId().equals(gid)){
                    if(user!=null){
                        if(!userService.delShopCar(shopCt.get(i), String.valueOf(user.getCid()))){
                            break;
                        }
                    }
                    shopCt.remove(shopCt.get(i));
                    responseMsg.setResult("0000");
                    String goods = getGoods(shopCt);
                    responseMsg.setResultMsg(goods);
                    return responseMsg;
                }
            }
        }
        responseMsg.setResult("0001");
        return responseMsg;
    }

    //添加一组购物项到购物车中
    @RequestMapping("addCar")
    public ResponseMsg addCar(String gid,String num, HttpSession session){
        ResponseMsg responseMsg = new ResponseMsg();
        Integer integer = null;
        try{
            integer = Integer.valueOf(num);
        }catch (Exception e){
            responseMsg.setResult("0001");
            return responseMsg;
        }
        if(gid!=null){
            List<GoodsInfo> shopCt = (List<GoodsInfo>) session.getAttribute("shopCt");
            UserInfo user = (UserInfo)session.getAttribute("user");
            if(null == shopCt){
                shopCt = new ArrayList<>();
            }
            GoodsInfo goodsInfo = null;
            for (int i = 0; i < shopCt.size();i++) {
                if(shopCt.get(i).getGoodsId().equals(gid)){
                    goodsInfo = shopCt.get(i);
                    goodsInfo.setGoodsNumber(goodsInfo.getGoodsNumber()+integer);
                    if(user!=null){
                        if(!userService.updShopCarNum(goodsInfo, String.valueOf(user.getCid()))){
                            goodsInfo.setGoodsNumber(goodsInfo.getGoodsNumber()-integer);
                            responseMsg.setResult("0001");
                            break;
                        }
                    }
                    responseMsg.setResult("0000");
                    break;
                }
            }
            a:if(goodsInfo == null){
                goodsInfo = goodsService.queryGoodsById(gid);
                goodsInfo.setGoodsNumber(integer);
                if(user!=null){
                    if(!userService.addShopCar(goodsInfo, String.valueOf(user.getCid()))){
                        responseMsg.setResult("0001");
                        break a;
                    }
                }
                shopCt.add(goodsInfo);
                responseMsg.setResult("0000");
            }
            session.setAttribute("shopCt",shopCt);
            String addGoods = getGoods(shopCt);
            responseMsg.setList(shopCt);
            responseMsg.setResultMsg(addGoods);
            return responseMsg;
        }
        responseMsg.setResult("0001");
        return responseMsg;
    }

    //给购物车中的一个购物项添加一
    @RequestMapping("addShop")
    public ResponseMsg addShop(String gid, HttpSession session){
        ResponseMsg responseMsg = new ResponseMsg();
        List<GoodsInfo> shopCt = (List<GoodsInfo>)session.getAttribute("shopCt");
        UserInfo user = (UserInfo)session.getAttribute("user");
        if(null == shopCt){
            shopCt = new ArrayList<>();
        }
        GoodsInfo goodsInfo = null;
        if(gid==null){
            responseMsg.setResult("0001");
        }
        for (int i = 0; i < shopCt.size();i++) {
            if(shopCt.get(i).getGoodsId().equals(gid)){
                goodsInfo = shopCt.get(i);
                goodsInfo.setGoodsNumber(goodsInfo.getGoodsNumber()+1);
                if(user!=null){
                    if(!userService.updShopCarNum(goodsInfo, String.valueOf(user.getCid()))){
                        goodsInfo.setGoodsNumber(goodsInfo.getGoodsNumber()-1);
                        responseMsg.setResult("0001");
                        break;
                    }
                }
                responseMsg.setResult("0000");
                break;
            }
        }
        a:if(goodsInfo == null){
            goodsInfo = goodsService.queryGoodsById(gid);
            if(user!=null){
                if(!userService.addShopCar(goodsInfo, String.valueOf(user.getCid()))){
                    responseMsg.setResult("0001");
                    break a;
                }
            }
            shopCt.add(goodsInfo);
            responseMsg.setResult("0000");
        }
        session.setAttribute("shopCt",shopCt);
        String addGoods = getGoods(shopCt);
        responseMsg.setList(shopCt);
        responseMsg.setResultMsg(addGoods);
        return responseMsg;
    }

    //给购物车中的一个购物项添减一
    @RequestMapping("subShop")
    public ResponseMsg subAdd(String gid, HttpSession session){
        ResponseMsg responseMsg = new ResponseMsg();
        if(gid!=null){
            List<GoodsInfo> shopCt = (List<GoodsInfo>) session.getAttribute("shopCt");
            UserInfo user = (UserInfo)session.getAttribute("user");
            for (int i = 0; i < shopCt.size(); i++) {
                if(shopCt.get(i).getGoodsId().equals(gid)){
                    if(shopCt.get(i).getGoodsNumber()>1){
                        shopCt.get(i).setGoodsNumber(shopCt.get(i).getGoodsNumber()-1);
                        if(user!=null){
                            if(!userService.updShopCarNum(shopCt.get(i), String.valueOf(user.getCid()))){
                                shopCt.get(i).setGoodsNumber(shopCt.get(i).getGoodsNumber()+1);
                                responseMsg.setResult("0001");
                                break;
                            }
                        }
                    }else{
                        if(user!=null){
                            if(!userService.delShopCar(shopCt.get(i), String.valueOf(user.getCid()))){
                                responseMsg.setResult("0001");
                                break;
                            }
                        }
                        shopCt.remove(shopCt.get(i));
                    }
                    responseMsg.setResult("0000");
                    String goods = getGoods(shopCt);
                    responseMsg.setList(shopCt);
                    responseMsg.setResultMsg(goods);
                    return responseMsg;
                }
            }
        }
        responseMsg.setResult("0001");
        return responseMsg;
    }

    //获取购物车中的总价和总数
    private String getGoods(List<GoodsInfo> shopCt){
        int number = 0;
        float count = 0;
        float subcount = 0;
        for (int i = 0; i < shopCt.size();i++) {
            number+=shopCt.get(i).getGoodsNumber();
            count+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber();
            subcount+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber()*shopCt.get(i).getGoodsDiscount();
        }
        return number+","+count+","+subcount;
    }

}
